import http from '../service/http.js'
function login(e, that,cb) {
    if (e.detail.userInfo) {
        that.setData({
            shouquan: 0
        })

        wx.login({
            success: function (res) {
                if (res.code) {
                    var udata = {
                        code: res.code,
                        s: 1
                    }
                    console.log('login code')
                    console.log(res);
                    http.req('/Wxapi/getOpenId', udata, function (res) {
                        console.log('openid retuirnedre');
                        console.log(res);
                        if (res) {

                            wx.getUserInfo({
                                success: function (userres) {
                                    console.log('userinfo is ')
                                    console.log(userres.userInfo)
                                    wx.setStorageSync('userinfo', userres.userInfo);
                                    var edata = {
                                        openid: res.openid,
                                        unionid: res.unionid,
                                        wx_name: userres.userInfo.nickName,
                                        wx_img: userres.userInfo.avatarUrl
                                    }
                                    console.log(edata)
                                    //return
                                    http.req('/Wxapi/add_user', edata, function (res) {
                                        edata.userid = res
                                        wx.setStorageSync('user', edata);
                                        return typeof cb == "function" && cb(res)
                                    })
                                }

                            })
                        }

                    });
                }
            },

        });
    } else {
        that.setData({
            shouquan: 1
        })
    }
}
module.exports = {
    login: login
}
